package top.yuyufeng.web.mobileServlet;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author yuyufeng
 * @date 2018/1/22
 */
@WebServlet(name = "ValidImageJsonServletMB", urlPatterns = "/validImageJsonMB")
public class ValidImageJsonServletMB extends HttpServlet {
    //312 155

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        Random random = new Random();

        File root = new File("c://test/upload_mb");
        File[] files = root.listFiles();
        int fileI = random.nextInt(files.length);
        String image = files[fileI].getName();

        //x轴在80-257之间
        int x = random.nextInt(257 - 80) + 80;
        int y = random.nextInt(100);
        System.out.println("location:" + x + " " + y);
        String location = x + "," + y;

        Map<String, String> map = new HashMap<String, String>();
        map.put("image", image);
        map.put("location", location);

        JSONObject jsonResult = new JSONObject();
        jsonResult.put("status", 200);
        jsonResult.put("locationY", y);


        HttpSession session = req.getSession();
        session.setAttribute("validMap", map);

        Writer writer = response.getWriter();
        writer.write(jsonResult.toString());
        writer.flush();
    }
}
